package com.jnu.testdrivendevelopment.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import androidx.annotation.NonNull;

import com.jnu.testdrivendevelopment.data.CirleSpriter;
import com.jnu.testdrivendevelopment.data.DataBank;

import java.util.ArrayList;
import java.util.TreeMap;

public class GameView extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder surfaceHolder;

    public GameView(Context context) {
        super(context);
        initView();
    }

    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public GameView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public GameView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView();
    }

    private void initView() {
        surfaceHolder.getSurface();
        surfaceHolder.addCallback(this);
    }

    private DrawThread drawThread;
    private boolean isTouched=true;
    private float touchedX=-1,touchedY=-1;
    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        drawThread = new DrawThread();
        drawThread.start();
        GameView.this.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(MotionEvent.ACTION_DOWN==event.getAction()){
                    touchedX=event.getRawX();
                    touchedY=event.getRawY();
                }
                return false;
            }
        });
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
        drawThread.myStop();
        try {
            drawThread.join();
        }catch (Exception e){
            e.printStackTrace();
        }
    }



    private class DrawThread extends Thread{

        private ArrayList<CirleSpriter> spriters=new ArrayList<>();
        private boolean isStopped=false;
        public void myStop(){
            isStopped=true;
        }

        public DrawThread(){
            spriters.add(new CirleSpriter(100,100,100,GameView.this.getWidth(),GameView.this.getHeight()));
            spriters.add(new CirleSpriter(300,300,100,GameView.this.getWidth(),GameView.this.getHeight()));
        }
        @Override
        public void run() {
            super.run();
            Canvas canvas=null;
            int bitCount=0;

            if(!isStopped){
                try {
                    canvas=surfaceHolder.lockCanvas();
                    canvas.drawColor(Color.GRAY);
                    if(GameView.this.isTouched){
                        for(int index=0;index<spriters.size();index++){
                            if(spriters.get(index).isShot(GameView.this.touchedX,GameView.this.touchedY)){
                                bitCount++;
                            }

                        }
                    }
                    for(int index=0;index<spriters.size();index++){
                        spriters.get(index).move();
                    }
                    for(int index=0;index<spriters.size();index++){
                        spriters.get(index).draw(canvas);
                    }
                    GameView.this.isTouched=false;
                    Paint paint=new Paint();
                    paint.setTextSize(100);
                    paint.setColor(Color.RED);
                    canvas.drawText("hit"+bitCount,100,100,paint);
                }catch (Exception e){

                }finally {
                    {
                        if(null!=canvas){
                            surfaceHolder.unlockCanvasAndPost(canvas);
                        }
                    }
                }
                try {
                    Thread.sleep(20);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
}
